package com.example.demo.controller;

import com.example.demo.entity.Login;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            Login login = (Login) subject.getPrincipal();
            subject.getSession().setAttribute("login", login);
            return "登录成功";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            return "用户名错误";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            return "密码错误";
        }
    }


}
